Skip to main content

Evaluation Metrics

OpenCLIP uses several standard metrics to evaluate model performance on zero-shot classification and retrieval tasks.

Classification Metrics

Top-1 Accuracy

Top-1 accuracy is the primary metric for classification tasks. It measures the percentage of samples where the model’s highest-confidence prediction matches the ground truth label.
Formula:
Example: If a model correctly classifies 633 out of 1000 images, its top-1 accuracy is 63.3%.

Top-5 Accuracy

Top-5 accuracy is more lenient—it considers a prediction correct if the ground truth label appears in the model’s top 5 predictions. Formula:
Use Case: Top-5 is useful when:
  • Classes are visually similar (e.g., dog breeds)
  • The task has high inherent ambiguity
  • Comparing models that might have similar top-1 but different top-5 performance

Per-Class vs. Overall Accuracy

OpenCLIP reports overall accuracy averaged across all samples. For class-imbalanced datasets, you might also want to compute per-class accuracy and take the mean.

Zero-Shot Accuracy Computation

Zero-shot accuracy in OpenCLIP is computed as follows:

1. Text Classifier Construction

For each class, generate multiple text embeddings using prompt templates:

2. Image Encoding

Encode the test image:

3. Similarity Computation

Compute cosine similarity between image and all class embeddings:
The temperature scaling factor of 100.0 is used to sharpen the probability distribution.

4. Accuracy Calculation

Compare predictions to ground truth:

Retrieval Metrics

For image-text retrieval tasks (like Flickr30k and MSCOCO), OpenCLIP uses standard retrieval metrics:

Recall@K

Recall@K measures the percentage of queries where the correct item appears in the top K retrieved results. Formula:
Common values:
  • R@1: Strictest metric (correct item must be rank 1)
  • R@5: Correct item in top 5
  • R@10: Correct item in top 10

Image-to-Text Retrieval

Given an image, retrieve relevant text captions:

Text-to-Image Retrieval

Given a text query, retrieve relevant images:

Mean Rank

Mean rank measures the average position of the correct item in the ranked list:
Lower mean rank is better.

Aggregate Metrics

Average Performance Across Datasets

The “Average perf. on 38 datasets” metric in our results is computed as:
This provides a single number summarizing model performance across the diverse evaluation suite.

Weighted Average

Some benchmarks use weighted averages where larger datasets have more influence:
OpenCLIP typically reports unweighted averages to give equal importance to each dataset.

Logging Metrics

OpenCLIP automatically logs metrics to your configured logging backend during training.

TensorBoard

Enable TensorBoard logging:
View metrics:
Metrics logged:
  • train/loss: Training loss per step
  • train/learning_rate: Current learning rate
  • imagenet-zeroshot-val-top1: Zero-shot ImageNet top-1 accuracy
  • imagenet-zeroshot-val-top5: Zero-shot ImageNet top-5 accuracy

Weights & Biases (wandb)

Enable wandb logging:
Metrics are automatically synced to your wandb dashboard with:
  • Real-time loss curves
  • Zero-shot accuracy over time
  • System metrics (GPU utilization, etc.)
For older runs (before PR #613), use the step variable instead of Step in wandb, as the latter was not properly set.

Custom Metrics

You can add custom metrics by modifying the training loop:

Metric Interpretation

ImageNet Zero-Shot Accuracy

Top-1 vs Top-5 Gap

The gap between top-1 and top-5 accuracy indicates:
  • Small gap (< 15%): Model is confident and accurate
  • Large gap (> 25%): Model often has correct answer in top 5 but not top 1, suggesting uncertainty or ambiguous classes

Cross-Dataset Performance

Strong models should maintain performance across datasets:
  • Consistent: Good performance across all 38 datasets
  • Specialized: High performance on some datasets but lower on others
  • Overfit: High ImageNet but low on distribution shift datasets

Computing Your Own Metrics

Using CLIP Benchmark

Custom Evaluation Loop

Best Practices

Use Standard Metrics: Stick to top-1, top-5, and recall@K for comparability with other work.
Report Multiple Datasets: ImageNet alone doesn’t tell the full story. Report performance on distribution shift and specialized datasets.
Log Frequently: Use --zeroshot-frequency 1 to track metrics every epoch during training.
Avoid Test Set Leakage: Always evaluate on validation or test sets that weren’t seen during training.

Next Steps